package com.gan.yidademo.common.base;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RestResultGenerator {

    public static final String SUCCESS = "success";

    public static final String ERROR = "error";

    public static final Integer SUCCESS_CODE = 600;

    public static final String SUCCESS_MESSAGE = "操作成功!";

    public static final Integer ERROR_CODE = -200;

    public static final String ERROR_MESSAGE = "服务器繁忙!";

    public static final Integer UN_LOGIN_CODE = 40001;

    public static final String UN_LOGIN_MESSAGE = "用户未登录!";

    /**
     * 生成响应成功的(不带正文)的结果
     *
     * @param message 成功提示信息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success(String message) {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(SUCCESS_CODE);
        result.setMessage(message);
        return result;
    }

    /**
     * 生成响应成功的(什么也不带的)的结果
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success() {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(SUCCESS_CODE);
        result.setMessage(SUCCESS_MESSAGE);
        return result;
    }

    /**
     * 生成响应成功(不带正文)的结果
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(SUCCESS_CODE);
        result.setData(data);
        result.setMessage(SUCCESS_MESSAGE);
        return result;
    }

    /**
     * 生成响应成功(带正文)的结果
     *
     * @param data    结果正文
     * @param message 成功提示信息
     * @return ResponseResult<T>
     */
    public static <T> ResponseResult<T> success(T data, String message) {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(SUCCESS_CODE);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    /**
     * 生成默认的响应失败的结果
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> error() {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(ERROR_CODE);
        result.setMessage(ERROR_MESSAGE);
        log.error("--------> result:{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 生成响应失败的结果
     *
     * @param message 自定义错误信息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> error(String message) {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(ERROR_CODE);
        result.setMessage(message);
        log.error("--------> result:{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 生成响应失败(带errorCode)的结果
     *
     * @param responseErrorEnum 失败信息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> error(ResponseErrorEnum responseErrorEnum) {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(responseErrorEnum.getCode());
        result.setMessage(responseErrorEnum.getMessage());
        log.error("--------> result:{}", JSONObject.toJSONString(result));
        return result;
    }


    public static <T> ResponseResult<T> error(ResponseErrorEnum responseErrorEnum, T data) {
        ResponseResult<T> result = ResponseResult.newInstance();
        result.setCode(responseErrorEnum.getCode());
        result.setMessage(responseErrorEnum.getMessage());
        result.setData(data);
        log.error("--------> result:{}", JSONObject.toJSONString(result));
        return result;
    }

    public static ResponseResult<?> error(Integer code, String message) {
        ResponseResult<?> result = ResponseResult.newInstance();
        result.setCode(code);
        result.setMessage(message);
        log.error("--------> result:{}", JSONObject.toJSONString(result));
        return result;
    }

}
